package com.arcsoft.crud.util;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Properties;
 
import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;
 

import com.arcsoft.crud.Constants.Global;
import com.mysql.jdbc.log.Log;
import com.mysql.jdbc.log.LogFactory;
 
/**
 * tomcat数据库连接池管理类<br>
 * 使用为tomcat部署环境<br>
 * 需要在类路径下准备数据库连接配置文件dbcp.properties
 * 
 * @author 宋信强
 * @mail songxinqiang123@gmail.com
 * 
 * @time 2013-12-27
 * 
 */
public class DBManager {
   // private static final Log log = LogFactory.getLog(DBManager.class);
    private static final String configFile = "config.properties";
 
    private static DataSource dataSource;
 
    static {
        Properties dbProperties = new Properties();
        try {
           
        	BasicDataSource ds = new BasicDataSource();
        	ds.setDriverClassName(Global.DRIVERCLASS);  
            ds.setUsername(Global.USERNAME);  
            ds.setPassword(Global.PASSWORD);  
            ds.setUrl(Global.CONNECTURL);  
            ds.setInitialSize(Global.INITCONN);
            ds.setMaxOpenPreparedStatements(0);
            ds.setMaxActive(Global.MAXACTIVE);
            ds.setMaxIdle(Global.MAXIDLE);  
            ds.setMaxWait(Global.MAXWAIT*1000);
           // ds.setMaxWait(Global.MAXWAIT); 
            //ds.setMaxWaitMillis(Global.MAXWAIT*1000);
            ds.setRemoveAbandonedTimeout(Global.REMOVEABANDONEDTIMEOUT);
            dataSource = ds;
            Connection conn = getConn();
            DatabaseMetaData mdm = conn.getMetaData();
            
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
           // log.error("初始化连接池失败：" + e);
        }
    }
 
    private DBManager() {
    }
 
    /**
     * 获取链接，用完后记得关闭
     * 
     * @see {@link DBManager#closeConn(Connection)}
     * @return
     */
    public static final Connection getConn() {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
        } catch (SQLException e) {
          //  log.error("获取数据库连接失败：" + e);
        }
        return conn;
    }
 
    /**
     * 关闭连接
     * 
     * @param conn
     *            需要关闭的连接
     */
    public static void closeConn(Connection conn) {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.setAutoCommit(true);
                conn.close();
            }
        } catch (SQLException e) {
            //log.error("关闭数据库连接失败：" + e);
        }
    }
 
}
